<?php
namespace app\common\taglib;
use think\template\TagLib;
use think\facade\Db;

class qh extends TagLib{
    /**
     * 定义标签列表
     */
    protected $tags   =  [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'close'     => ['attr' => 'time,format', 'close' => 0], //闭合标签，默认为不闭合
        'open'      => ['attr' => 'name,type', 'close' => 1], 
        'get'       => ['attr'=> 'sql,where,order,value,key,limit,page,field','close'=>1],
        'navs'       => ['attr'=> 'id,limit,where', 'close' => 1],
    ];

    /**
     * 这是一个闭合标签的简单演示
     */
    public function tagClose($tag)
    {
        $format = empty($tag['format']) ? 'Y-m-d H:i:s' : $tag['format'];
        $time = empty($tag['time']) ? time() : $tag['time'];
        $parse = '<?php ';
        $parse .= 'echo date("' . $format . '",' . $time . ');';
        $parse .= ' ?>';
        return $parse;
    }
    
    /**
     * 这是一个非闭合标签的简单演示
     */
    public function tagOpen($tag, $content)
    {
        $type = empty($tag['type']) ? 0 : 1; // 这个type目的是为了区分类型，一般来源是数据库
        $name = $tag['name']; // name是必填项，这里不做判断了
        $parse = '<?php ';
        $parse .= '$test_arr=[[1,3,5,7,9],[2,4,6,8,10]];'; // 这里是模拟数据
        $parse .= '$__LIST__ = $test_arr[' . $type . '];';
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    
    
    //通用导航信息
    public function tagNavs($tag,$content){
        $tag['limit'] = isset($tag['limit']) ? $tag['limit'] : '0';
        $tag['id']    = isset($tag['id'])    ? $tag['id']    : '';
        $name         = isset($tag['name'])  ? $tag['name']  : 'nav';
        $where = isset($tag['where']) ? $tag['where'] : '';

        if(!empty($tag['id'])){
            $catestr = '$__CATE__ = D(\'category\')->where(\'display\',1)->order(\'listorder ASC,id DESC\')->select();';
            $catestr.= '$__LIST__ = getChildsOn($__CATE__,'.$tag['id'].');';
        }else{
            $catestr = '$__CATE__ = D(\'category\')->where(\'display\',1)->order(\'listorder ASC,id DESC\')->select();';
            $catestr.= '$__LIST__ = unlimitedForLayer($__CATE__);';
        }
        //提取前N条数据,因为sql的LIMIT避免不了子栏目的问题
        if(!empty($tag['limit'])){
            $catestr.= '$__LIST__ =  array_slice($__LIST__, 0,'.$tag['limit'].');';
        }
        $parse = '<?php ';
        $parse .= $catestr;
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    
    
    public static function tagGet($attr, $content){
        $sql = !empty($attr['sql']) ? $attr['sql'] : '';
        $limit = !empty($attr['limit']) ? $attr['limit'] : '10';
        $field = !empty($attr['field']) ? $attr['field'] : '';
        $where = !empty($attr['where']) ? $attr['where'] : '';
        $page = !empty($attr['page']) ? $attr['page'] : '';
        $order = !empty($attr['order']) ? $attr['order'] : 'id DESC';
        $value = !empty($attr['value']) ? $attr['value'] : 'v';
        $key   = !empty($attr['key'])   ? $attr['key'] : 'i';
        $html  = '<?php ';
        $html .= '$list_where = "'.$where.'";';
        if(!empty($attr['page'])){
        $html .= '$list_db_info = think\facade\Db::table(\''.$sql.'\')->field(\''.$field.'\')->where($list_where)->order(\''.$order.'\')->paginate('.$limit.');';
        $html .= '$list_page = $list_db_info->render();';
        }
        else{
          $html .= '$list_db_info = think\facade\Db::table(\''.$sql.'\')->field(\''.$field.'\')->where($list_where)->order(\''.$order.'\')->limit('.$limit.')->order(\''.$order.'\')->select();';  
        }
        $html .= ' ?>';
        # 开启循环
        $html .= '{volist name="list_db_info" id="' . $value . '" key="' . $key . '"}';
        $html .= $content;
        $html .= '{/volist}';
        return $html;
    }
   
}